package com.friends.digitpic.controller;

import com.friends.common.tools.util.HttpMessage;
import com.friends.digitpic.business.service.UserBusinessService;
import com.friends.digitpic.service.entity.UserEntity;
import com.friends.digitpic.service.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by fusio on 2018/9/15.
 */
@Controller
@RequestMapping("/account")
public class AccountController {

    @Resource
    private UserService userService;

    @Resource
    private UserBusinessService userBusinessServicee;

    /**
     * 注册账户
     * @param usertype
     * @param username
     * @param nickname
     * @param pwd
     * @param mobile
     * @param mail
     * @return
     */
    @RequestMapping("/regAccount.html")
    @ResponseBody
    public HttpMessage regAccount(
            String usertype,
            String username,
            String nickname,
            String pwd,
            String mobile,
            String mail){

        UserEntity entity = new UserEntity();
        entity.setUsertype(usertype);
        entity.setUsername(username);
        entity.setUsernickname(nickname);
        entity.setUserpwd(pwd);
        entity.setUsermobile(mobile);
        entity.setUsermail(mail);
        userService.insertUser(entity);
        return  HttpMessage.createSuccessMsg(entity);
    }


    /**
     * 账户登录
     * @param loginid
     * @param loginpwd
     * @param request
     * @return
     */
    @RequestMapping("/login.html")
    @ResponseBody
    public HttpMessage login(
            @RequestParam(value = "loginid", required = true)
            String loginid,
            @RequestParam(value = "loginpwd", required = true)
            String loginpwd,
            HttpServletRequest request
    ){
        UserEntity entity = userBusinessServicee.validateUserPassword(loginid,loginpwd);
        if(entity!=null) {
            request.getSession().setAttribute("user", entity);
            return HttpMessage.createSuccessMsg(entity);
        }

        return HttpMessage.createErrorMsg("用户名或者密码错误");
    }

    /**
     * 账户登出
     * @param loginid
     * @return
     */
    @RequestMapping("/logout.html")
    @ResponseBody
    public HttpMessage logout(
            @RequestParam(value = "loginid", required = true)
                              String loginid){

        return HttpMessage.createSuccessMsg("");
    }

    /**
     * 查询用户信息
     * @param userid
     * @return
     */
    @RequestMapping("/getUserInfo.html")
    @ResponseBody
    public HttpMessage getUserInfo(
            @RequestParam(value = "userid", required = true)
            String userid
    ){
        UserEntity entity = userService.getUser(userid);
        if (entity!=null)
            return HttpMessage.createSuccessMsg(entity);
        return  HttpMessage.createErrorMsg("未查询到用户信息");
    }




    public HttpMessage bindQQ(
            String qq,
            String username,
            String pwd
    ){
        return HttpMessage.createErrorMsg("");
    }
}
